package util;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.alibaba.fastjson.JSON;

/**
 * 
 * @author 		周路
 * @TimeApp   		2011/11/05 17:01
 * @see			
 * @category
 * @version		2012/03/06  第4次修定 = 2012/03/30  第5次修定
 */
public class SessionJson {

		private static Writer 				write;
		private static PrintWriter 			printWriter;
		private static JSON 				json;
		private static String 				name;
		private static Object 				object;
		private static List					list;

		
		
/**
 * @author 输出
 * 
 * @param jsonstr
 */
public static void JsonWrite(String jsonstr){
	ActionContext context= ActionContext.getContext();

	HttpServletRequest request= (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
	HttpServletResponse response=(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);
	PrintWriter 		out = null;
	try {	response.setCharacterEncoding("utf-8");
			out =response.getWriter();
			out.write(jsonstr);
		}
	catch(IOException e) {System.out.print("方法异常,请定位问题,及时处理！");	}
	catch(Exception exception){System.out.print("方法异常,请定位问题,及时处理！");	}
	finally{
		out.close();
	}
}

public static void JsonpWrite(String jsonstr){
	HttpServletResponse response;
	PrintWriter out;
	ActionContext context = ActionContext.getContext();
	HttpServletRequest request = (HttpServletRequest)context.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
	response = (HttpServletResponse)context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");
	out = null;
	try {	
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/javascript; charset=UTF-8");
		out = response.getWriter();
		String jsonp = (new StringBuilder("try {callback_function(")).append(jsonstr).append(");} catch (e) { alert(\"fhrnsrsbh=\"+fhrnsrsbh+\"\\n\"+e);}").toString();
		System.out.println((new StringBuilder("jsonp=\n")).append(jsonp).toString());
		out.write(jsonp);
		}
	catch(IOException e) {
		System.out.print("方法异常,请定位问题,及时处理！");	
		}
	catch(Exception exception){
		System.out.print("方法异常,请定位问题,及时处理！");	
		}
	finally{
		out.close();
	}
}
/**
 * 
 * @param KeyStr
 * @param list
 */
public static void PutList(String KeyStr,List list){
	ActionContext.getContext().getSession().put(KeyStr, list);}
/**
 * 
 * @param Key
 * @param KeyValue
 */
public static void PutStr(String Key,String KeyValue){
	ActionContext.getContext().getSession().put(Key, KeyValue);}
public static void PutInt(String Key,Integer KeyValue){
	ActionContext.getContext().getSession().put(Key, KeyValue);}

/**
	 * 
	 * @param 	obj
	 * @return 	void
	 */
public static void PutObj(String objKey,Object obj){
			try {
				ActionContext.getContext().getSession().put(objKey,obj);}
			catch (Exception e) {e.printStackTrace();System.out.println("方法出错或没找到 ！"+obj);}
}
/**
 * 
 * @param 	Key
 * @return 	String
 */
public static String getStr(String Key){
		try {name=(String) ActionContext.getContext().getSession().get(Key);}
		catch (Exception e) {System.out.println("方法出错或没找到 ！"+Key);}
		return name;
}
	
/**
 * 
 * @param 	Listkey
 * @return	List
 */
public static List getList(String Listkey){
		try {list=(List) ActionContext.getContext().getSession().get(Listkey);}
		catch (Exception e) {e.printStackTrace();System.out.println("方法出错或没找到 ！"+Listkey);}
		return list;
}

/**
 * 
 * @param 	obj
 * @return 	Object
 */
public static Object getObj(String obj){
		try {object=(Object) ActionContext.getContext().getSession().get(obj);}
		catch (Exception e) {e.printStackTrace();System.out.println("方法出错或没找到 ！"+obj);}
		return object;
}
/**
 * @type  List
 * @param list
 * @return
 */
public static String getJsonStr(List list){
	return JSON.toJSONString(list);
}

/**
 * @type Object
 * @param obj
 * @return
 */
public static String getJsonStr(Object obj){
	return JSON.toJSONString(obj);
}

public  Writer getWrite() {return write;}
public  JSON getJson() {return json;}
public  void setJson(JSON json) {
	SessionJson.json = json;}
}